package com.credithc.signature.common.utils.sequence;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述：<br/>
 * 作者：sunyang
 * 创建：2019/8/26 10:17
 * 版本：v1.0.0<br/>
 */
public class BaseSequenceManager implements ISequenceManager {

    private Map<String, ISequenceGenerater> map;

    private BaseSequenceManager() {
        this.map = new ConcurrentHashMap<String, ISequenceGenerater>();
    }

    private static final class ManagerHolder {
        public static ISequenceManager ism = new BaseSequenceManager();

    }

    public static ISequenceManager getInstance() {
        return ManagerHolder.ism;
    }

    @Override
    public synchronized ISequenceGenerater getSequence(final String sequenceName, final SequenceType type, final long maxValue, final long increment) {
        if (this.map.containsKey(sequenceName)) {
            return this.map.get(sequenceName);
        }
        if (type.equals(SequenceType.Simple)) {
            final ISequenceGenerater temp = new SimpleSequence(sequenceName, maxValue, increment);
            this.map.put(sequenceName, temp);
            return temp;
        }
        return null;
    }

    public enum SequenceType
    {
        Simple("Simple", 1);

        SequenceType(final String s, final int n) {

        }
    }
}
